/*
 * Copyright (c) 2017 zhangcm. All Rights Reserved.
 */
package com.jnzc.ocr.serv.shop.helper;

import com.google.common.collect.Lists;
import com.jnzc.ocr.common.error.BusinessException;
import com.jnzc.ocr.common.utils.BigDecimalUtils;
import com.jnzc.ocr.common.utils.CalculateUtils;
import com.jnzc.ocr.common.utils.StringSubUtils;
import com.jnzc.ocr.serv.shop.consts.ShopMarketingTypeConsts;
import com.jnzc.ocr.serv.shop.entity.ShopOrderSaleDetail;
import com.jnzc.ocr.serv.shop.vo.ShopMarketingPlanSelectResultVo;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author zhangcm
 * @version 1.0, 2017/10/6
 */
public class ShopOrderSaleHelper {

    private ShopOrderSaleHelper(){};

    public static String getOrderName(List<ShopOrderSaleDetail> detailList) {
        List<String> detailNameList = Lists.newArrayList();
        for (ShopOrderSaleDetail obj : detailList) {
            StringBuilder sb = new StringBuilder(obj.getProductName());
            if(StringUtils.isNotEmpty(obj.getSkuName())){
                sb.append("," + obj.getSkuName());
            }
            sb.append("*" + obj.getNum());
            detailNameList.add(sb.toString());
        }
        String result = StringUtils.join(detailNameList, ",");
        return StringSubUtils.get(result, 200, "...");
    }

    public static BigDecimal getTotalSaleAmount(List<ShopOrderSaleDetail> detailList){
        BigDecimal result = BigDecimal.ZERO;
        for (ShopOrderSaleDetail detail : detailList) {
            result = BigDecimalUtils.add(result, detail.getSaleAmount());
        }
        return result;
    }

    public static BigDecimal getTotalDiscountAmount(List<ShopOrderSaleDetail> detailList){
        BigDecimal result = BigDecimal.ZERO;
        for (ShopOrderSaleDetail detail : detailList) {
            result = BigDecimalUtils.add(result, detail.getDiscountAmount());
        }
        return result;
    }

    public static BigDecimal getTotalAmount(List<ShopOrderSaleDetail> detailList){
        BigDecimal totalSaleAmount = ShopOrderSaleHelper.getTotalSaleAmount(detailList);
        BigDecimal totalDiscountAmount = ShopOrderSaleHelper.getTotalDiscountAmount(detailList);
        return BigDecimalUtils.sub(totalSaleAmount, totalDiscountAmount);
    }

    public static BigDecimal getPlanAmount(ShopMarketingPlanSelectResultVo plan, BigDecimal amount){
        if(plan.getPlan().getMarketingType().equals(ShopMarketingTypeConsts.AMOUNT)) {
            return plan.getRule().getAmount();
        } else if(plan.getPlan().getMarketingType().equals(ShopMarketingTypeConsts.DISCOUNT)) {
            BigDecimal discount = CalculateUtils.sub(10, plan.getRule().getDiscount());
            return CalculateUtils.div(CalculateUtils.mul(amount, discount), 10, 2);
        } else {
            throw new BusinessException("类型不正确");
        }
    }

    public static Integer getTotalNum(List<ShopOrderSaleDetail> detailList){
        Integer result = 0;
        for (ShopOrderSaleDetail detail : detailList) {
            result = CalculateUtils.add(result, detail.getNum());
        }
        return result;
    }

    public static Integer getTotalRefundNum(List<ShopOrderSaleDetail> detailList){
        Integer result = 0;
        for (ShopOrderSaleDetail detail : detailList) {
            result = CalculateUtils.add(result, detail.getRefundNum());
        }
        return result;
    }
}
